package com.ruoyi.web.controller.wxqy;

import com.ruoyi.common.annotation.ControllerAuthority;
import com.ruoyi.common.constant.ErrorConstants;
import com.ruoyi.common.core.cache.Meta;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.NumberUtils;
import com.ruoyi.common.utils.StrUtils;
import com.ruoyi.qywx.model.vo.BindTagVo;
import com.ruoyi.qywx.model.vo.CustomerFollowerVo;
import com.ruoyi.qywx.model.vo.CustomerQueryVo;
import com.ruoyi.qywx.service.inters.CustomerService;
import com.ruoyi.system.service.ISysTeamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * @author : hcl
 * @Date : 2023/8/29 16:39
 * 客户管理
 */
@RestController
@ControllerAuthority
@RequestMapping("/wxqy/customer")
public class CustomerController extends BaseController {
    @Autowired
    private CustomerService customerService;
    @Autowired
    private ISysTeamService teamService;

    @GetMapping("/list")
    public AjaxResult list(CustomerQueryVo vo) {
        startPage();
        return success(getDataTable(customerService.list(vo, getTeamId())));
    }

    @GetMapping("/detail")
    public AjaxResult detail(Long id) {
        if (NumberUtils.isFalse(id)) {
            return error(ErrorConstants.NO_DATA);
        }
        return customerService.findCustomerById(id, getTeamId());
    }

    @PostMapping("/bind-tags")
    public AjaxResult customerBindTags(@Valid BindTagVo bind) {
        if (NumberUtils.isFalse(bind.getId()) || NumberUtils.isFalse(bind.getType())) {
            return error(ErrorConstants.PARAMS_ERR);
        }
        Meta meta = teamService.selectTeamMeta(getTeamId());
        return customerService.customerBindTags(bind, getTeamMeta());
    }

    @PostMapping("/edit/remark")
    public AjaxResult editRemark(CustomerFollowerVo vo) {
        customerService.editRemark(vo, getTeamMeta());
        return success();
    }

    private Meta getTeamMeta() {
        Meta meta = teamService.selectTeamMeta(getTeamId());
        if (meta == null) {
            throw new ServiceException(ErrorConstants.META_NOT_FOUND);
        }
        return meta;
    }

}
